package com.crm.web.controller;

import com.crm.entity.Product;
import com.crm.service.ProductService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Resource
    ProductService productService;


    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       @RequestParam(defaultValue = "",required = false) String prodName,
                       @RequestParam(defaultValue = "",required = false)String prodType,
                       @RequestParam(defaultValue = "",required = false)String prodBatch) {
        Sort sort = new Sort(Sort.Direction.ASC, "prodId");
        Pageable pageable = PageRequest.of(pageIndex - 1, 3, sort);
        Page<Product> page = productService.findAllByPage(pageable, prodName, prodType, prodBatch);
        model.addAttribute("productPage",page);
        model.addAttribute("prodName",prodName);
        model.addAttribute("prodType",prodType);
        model.addAttribute("prodBatch",prodBatch);
        return "product/list";
    }

}
